<?php
/**
* @author Oliver Leisalu, Inditel Meedia
* @package DomFg
* @subpackage Cells
*/
class DomFgCalendar extends DomFgComponent {
	
	protected $dayHandler;
	
	protected $start;
	
	protected $end;
	
	protected $monthNames = array('Jaanuar','Veebruar','Märts','Aprill','Mai','Juuni','Juuli','August','September','Oktoober','November','Detsember');
	
	protected $monthStyle;
	
	public function __construct() {
		$this->start = date('Y-m-d');
		$this->end = date('Y-m-d', strtotime('+60 days'));
	}
	
	/**
	 * This function will be called for every day in calendar.<br>
	 * Sample callback function:<br>
	 * <code>
	 * function dayHandler( $stamp ) {
	 * 		return date('Y-m-d', $stamp );
	 * }
	 * </code>
	 * @param String $functionName
	 */
	public function dayHandler( $handler ) {
		if( is_object($handler) && !($handler instanceof DomFgCalendarDayHandler) )
			throw new RuntimeException('DomFgCalendar day handler must be instanceof DomFgCalendarDayHandler or function name!');
		$this->dayHandler = $handler;
	}
	/**
	 * These arguments will be given to day callback function as arguments. Notice that first argument is always stamp!
	 * @param array $args
	 */
	public function dayHandlerArgs( array $args ) {
		$this->dayHandlerArgs = $args;
	}
	
	/**
	 * @param String $date Format Y-m-d
	 */
	public function start( $date ) {
		$this->start = $date;
	}
	
	/**
	 * @param String $date Format Y-m-d
	 */
	public function end( $date ) {
		$this->end = $date;
	}
	
	public function monthStyle( $style ) {
		$this->monthStyle = $style;
	}
	
	public function render() {
		
		$startStamp = strtotime( $this->start );
		$endStamp = strtotime( $this->end );
		
		$startYear = (int)date('Y', $startStamp );
		$startMonth = (int)date('n', $startStamp );
		//$startDay = (int)date('j', $startStamp );
		
		$months = ceil( ($endStamp - $startStamp) / (86400*30) );
		
		$calendar = array();
		
		for( $m=0; $m<$months; $m++ ) {
			
			$monthFirstDayStamp = mktime( 0, 0, 1, $startMonth+$m, 1, $startYear );
			$days = date('t', $monthFirstDayStamp );
			$month = date('n', $monthFirstDayStamp );
			$year = date('Y', $monthFirstDayStamp );
			$monthLastDayStamp = mktime( 0, 0, 1, $startMonth+$m, $days, $startYear );
			
			$calendar[$m] = array(
				'name' => $this->monthNames[(int)$month-1].' '.$year,
				'month' => date('m',$monthFirstDayStamp),
			);
			
			// -------------------------------
			// MONTH DAYS LOOP (+ prev and next month days from start of week to end of week. )
			// This always works: (Total days)%7==0
			
			for( $d=2-date('N', $monthFirstDayStamp ), $showDays = $days + 7 - date('N',$monthLastDayStamp); $d<=$showDays; $d++) {
				
				$stamp = mktime(0,0,1,$startMonth+$m, $d, $startYear );
				
				$day = new DomFgCalendarDay( $stamp, $month );
				$day->setHandler( $this->dayHandler, $this->dayHandlerArgs );
				$day->render();
				
				$calendar[$m]['days'][] = $day;
				
			} // end for ($d)
			// -------------------------------
			
			
		}
		//debug($calendar);
		$renderer = $this->getRenderer();
		$renderer->assign('calendar', $calendar );
		$renderer->assign('cell', $this );
		return $renderer->fetch('Cells/Calendar.html');
	}
	
}
/**
* @author Oliver Leisalu, Inditel Meedia
* @package DomFg
* @subpackage Cells
*/
class DomFgCalendarDay {
	
	private $stamp;
	
	private $belongingMonth;
	
	public $contents;
	
	public $style;
	
	private $handler;
	
	private $handlerArgs;

	public $drawDayNr = true;
		
	public function __construct( $stamp, $belongingMonth ) {
		$this->stamp = (int)$stamp;
		$this->belongingMonth = (int)$belongingMonth;
	}
	
	public function setHandler( $handler, array $args ) {
		$this->handler = $handler;
		$this->handlerArgs = $args;
	}
	
	public function isCurrentMonthDay() {
		return $this->belongingMonth == (int)date('n', $this->stamp );
	}
	
	public function getDayOfWeek( ) {
		return date('N', $this->stamp );
	}
	
	public function getDay( ) {
		return date('j', $this->stamp );
	}
	
	public function inPast() {
		return date('Ymd',strtotime('now')) > date('Ymd', $this->stamp);
	}
	
	public function isToday() {
		return date('Y-m-d') == date('Y-m-d', $this->stamp);
	}
	
	public function isWorkDay( ) {
		$dayOfWeek  = $this->getDayOfWeek();
		return $dayOfWeek != 6 && $dayOfWeek != 7;
	}
	
	public function isWeekLastDay() {
		$dayOfWeek  = $this->getDayOfWeek();
		return $dayOfWeek == 7;
	}
	
	public function date( $format ) {
		return date($format, $this->stamp );
	}
	
	public function __get( $var ) {
		return $this->$var;
	}
	
	public function render() {
		if( !$this->handler )
			return;
		
		$this->contents = call_user_func_array( $this->handler, array_merge( array($this), $this->handlerArgs ) );
	}
	
	
}
?>